## Deserving Government Assistance? 
## Public Support for Aid to Struggling Firms and Workers
#############################################################
## Chris Witko
## Tobias Heinrich
#############################################################
## 2) Prepare survey data 
#############################################################


## Load survey data
###################
data_all <- read.csv("data/Bailout October 2021.csv", stringsAsFactors = FALSE)
data_all <- data_all[3:nrow(data_all), ]
data_all <- subset(data_all, Q25 == "Yes")

to_get_base <- c("Q6", "Q7", "Q8", "Q9", "Q10", "Q27", "Q11", "Q32", "Q12", "Q14",
                 "Q31", "Q15", "Q3", "Q5")
to_get_firms <- colnames(data_all)[grepl(x=colnames(data_all), pattern="F_attr")]
to_get_workers <- colnames(data_all)[grepl(x=colnames(data_all), pattern="W_attr")]
data_all$id <- 1:nrow(data_all)

## Recode demographic variables
data_base <- data_all[, c("id", to_get_base)]
data_base$Age <- (2021 - as.numeric(data_base$Q3)) / 100
data_base$Q3 <- NULL

data_base$Union_member <- mapvalues(x=data_base$Q27,
                                    from=c("I am not now, nor have I been, a member of a labor union.",
                                           "I formerly was a member of a labor union.",
                                           "Yes, I am currently a member of a labor union"),
                                    to=c("No", "In past", "Now"))
data_base$Q27 <- NULL

data_base$Job_type <- mapvalues(x=data_base$Q11,
                                from=c("Blue collar worker", "White collar worker",
                                       "Not sure", "Neither"),
                                to=c("Blue", "White", "Neither", "NS"))
data_base$Q11 <- NULL

data_base$Living_area <- data_base$Q15
data_base$Living_area[data_base$Living_area == "Other"] <- "Rural area"
data_base$Living_area[data_base$Living_area == ""] <- "Rural area"
data_base$Q15 <- NULL

data_base$Class <- str_remove_all(string=data_base$Q12, pattern=" class")
data_base$Q12 <- NULL

data_base$RaceEthnicity <- mapvalues(x=data_base$Q7,
                                     from=c("Asian", "Black", "Hispanic", "Middle Eastern",
                                            "Mixed", "Native American", "Other", "White"),
                                     to=c("Other", "Black", "Hispanic", "Other", "Other",
                                          "Other", "Other", "White"))
data_base$Q7 <- NULL

data_base$Gender_female <- ifelse(data_base$Q6 == "Female", 1, 0)
data_base$Q6 <- NULL

data_base$Education_uni <- ifelse(data_base$Q5 %in% c("Post-graduate", "4-year college"), 1, 0)
data_base$Q5 <- NULL

data_base$Job <- mapvalues(x=data_base$Q14, from=c("Full-time", "Homemaker", "Other",
                                                   "Part-time", "Permanently disabled",
                                                   "Retired", "Student", "Temporarily laid off",
                                                   "Unemployed", ""),
                           to=c("Full-time", "Other", "Other", "Part-time", "Other",
                                "Other", "Student", "Other", "Unemployed", "Other"))
data_base$Q14 <- NULL

data_base$Ideology <- mapvalues(x=data_base$Q8, 
                                from=c("", "Conservative", "Liberal", "Moderate",
                                       "Not sure", "Very conservative", "Very liberal"),
                                to=c("NS", "Conservative", "Liberal", "Moderate",
                                     "NS", "Conservative", "Liberal"))
data_base$Q8 <- NULL

data_base$Inequality <- mapvalues(x=data_base$Q32, from=c("Not at all concerned", "Not very concerned",
                                                          "Somewhat concerned", "Very concerned"),
                                  to=c("Not at all", "Not very", "Somewhat", "Very"))
data_base$Q32 <- NULL

data_base$Party <- data_base$Q9
data_base$Q9 <- NULL

data_base$Income <- data_base$Q10
data_base$Q10 <- NULL

data_base$Inequality_perception <- data_base$Q31
data_base$Q31 <- NULL


## Impute age for a few age observations
imp_f <- Age ~ 1 + factor(Union_member) + factor(Job_type) + factor(Living_area) + factor(Class) +
  factor(RaceEthnicity) + Gender_female + Education_uni + factor(Job) + factor(Ideology) +
  factor(Party) + factor(Income) + factor(Inequality_perception)

mod <- bayesglm(formula=imp_f, data=data_base)
miss <- which(is.na(data_base$Age))
data_base$Age[miss] <- -9
X <- model.matrix(imp_f, data=data_base)[miss, ]
data_base$Age[miss] <- c(coef(mod) %*% t(X))


## Extract all the conjoint information
#######################################
attr_raw <- list(Worker=list(Salary=c("$70,000", "$160,000", "$20,000"),
                             'Reason for job loss'=c("Laid off due to economic recession",
                                                     "Company was closed due to pandemic public health restrictions",
                                                     "Laid off due to company mismanagement",
                                                     "Job was replaced by technology (like robot, artificial intelligence)",
                                                     "Operations were moved to a foreign country",
                                                     "Fired for poor performance",
                                                     "Laid off due to foreign competition",
                                                     "Laid off due to domestic competition"),
                             'Race/ ethnicity'=c("White", "Black", "Asian", "Hispanic", 
                                                 "Native American", "Other", "Mixed"),
                             'Age'=c("20s", "30s", "40s", "50s", "60s"),
                             'Gender'=c("Male", "Female"),
                             'Education'=c("Less than 4-year university", "4-year university")),
                 Firm=list('Typical worker age'=c("20s", "30s", "40s", "50s", "60s"),
                           'Number of workers'=c("Between 5 and 9", "Between 10 and 19", 
                                                 "Between 20 and 99", "Between 100 and 499",
                                                 "500 or more", "Less than 5"),
                           'Reason for struggle'=c("Economic recession", "Foreign competition due to lower labor cost",
                                                   "Operations closed due to pandemic public health restrictions",
                                                   "Competition due to superior use of technology (like robots, artificial intelligence)",
                                                   "Mismanagement"),
                           'Typical worker salary'=c("$20,000", "$70,000", "$160,000"),
                           'Industry'= c("Arts, entertainment, recreation", "Construction", "Finance and insurance",
                                         "Health care", "Manufacturing", "Real estate and rental", "Retail",
                                         "Transportation and warehousing", "Travel and food services", "Wholesale trade"),
                           'Typical worker education'=c("4-year university", "Less than 4-year university"),
                           'CEO pay'=c("$200,000", "$10 million", "$2 million"),
                           'Typical worker race/ ethnicity'=c("Asian", "Black", "Hispanic", "Mixed", "Native American",
                                                              "Other", "White")))

attr_table <- c()
for(i in 1:2)
{
  for(j in 1:length(attr_raw[[i]]))
  {
    tmp <- data.frame(c_what=names(attr_raw)[i], c_attr_pretty=names(attr_raw[[i]])[j],
                      c_real_pretty=attr_raw[[i]][[j]])
    attr_table <- rbind(attr_table, tmp)
  }
}
attr_table$c_attr_short <- str_remove_all(string=attr_table$c_attr_pretty, pattern="/ ")
attr_table$c_attr_short <- str_remove_all(string=attr_table$c_attr_short, pattern=" ")
attr_table$c_attr_short <- tolower(attr_table$c_attr_short)

for(j in 1:nrow(attr_table))
{
  tag <- paste0(ifelse(attr_table$c_what[i] == "Firm", "F_", "W_"),
                attr_table$c_attr_short[i])
  data_all[, tag] <- NA
}


## Parse the results in a stacked, long format
data_parsed_tmp <- vector("list", 12*2)
count <- 1
for(j in 1:12)
{
  for(k in 1:2)
  {
    tmp <- data_all
    tmp$Round <- j
    tmp$Side <- ifelse(k == 1, "L", "R")
    
    for(m in 1:nrow(attr_table))
    {
      a_max <- ifelse(attr_table$c_what[m] == "Firm", 8, 6)
      for(l in 1:a_max)
      {
        c_what <- ifelse(attr_table$c_what[m] == "Firm", "F_", "W_")
        tag <- paste0(c_what, "attr_R", j, "_S", ifelse(k == 1, "L", "R"), "_A", l)
        ind <- tmp[, tag] == attr_table$c_real_pretty[m]
        tmp[ind, paste0(c_what, attr_table$c_attr_short[m])] <- attr_table$c_real_pretty[m]
      }
    }
    
    ## Choice
    tmp$Which_experiment <- what_type <- ifelse(tmp$X10_f_choice == "", "Worker", "Firm")
    tmp$Choice_raw <- ifelse(tmp$Which_experiment == "Worker", tmp[, paste0("X", j, "_w_choice")],
                             tmp[, paste0("X", j, "_f_choice")])
    tmp$Choice_raw <- str_remove(string=tmp$Choice_raw, pattern="Worker ")
    tmp$Choice_raw <- str_remove(string=tmp$Choice_raw, pattern="Company ")
    tmp$Choice <- ifelse(tmp$Side == "L" & tmp$Choice_raw == "Left", 1, 
                         ifelse(tmp$Side == "R" & tmp$Choice_raw == "Right", 1, 0))
    
    data_parsed_tmp[[count]] <- tmp
    count <- count + 1
    rm(tmp)
  }
}  
data <- do.call("rbind", data_parsed_tmp)
data[, grepl(x=colnames(data), pattern="F_attr")] <- NULL
data[, grepl(x=colnames(data), pattern="W_attr")] <- NULL
data[, grepl(x=colnames(data), pattern="w_choice")] <- NULL
data[, grepl(x=colnames(data), pattern="f_choice")] <- NULL
data$Choice_raw <- NULL

data2 <- cbind(data[, c("id", "Which_experiment", "Side", "Round", "Choice")],
               data[, grepl(x=colnames(data), pattern="F_")],
               data[, grepl(x=colnames(data), pattern="W_")])


## Merge together with base data
################################
data <- merge(data_base, data2, by="id", all.x=TRUE)


## Prepare CCES data
####################
CCES <- read.dta13("data/cces18_common_vv.dta")
CCES <- CCES[, c("commonweight", "birthyr", "employ", "gender", "pid3", "educ")]
CCES$Age <- (2018 - CCES$birthyr) / 100
CCES$Gender_female <- ifelse(CCES$gender == 1, 0, 1)
CCES$w <- CCES$commonweight

CCES$Education_uni <- ifelse(CCES$educ %in% c(5, 6), 1, 0)

CCES$Job <- mapvalues(x=CCES$employ, from=1:9,
                      to=c("Full-time", "Part-time", "Other",
                           "Unemployed", "Other", "Other", "Other",
                           "Student", "Other"))

CCES$Party <- mapvalues(x=CCES$pid3, from=1:5,
                        to=c("Democrat", "Republican", "Independent",
                             "Other", "Not sure"))

CCES$educ <- NULL
CCES$commonweight <- NULL
CCES$birthyr <- NULL
CCES$employ <- NULL
CCES$gender <- NULL
CCES$pid3 <- NULL
CCES <- na.omit(CCES)
CCES$Age <- round(CCES$Age, di=1)


## Save all to disk
###################
write.csv(x=data, file="output/Prepped_survey_data.csv")
write.csv(x=CCES, file="output/Prepped_CCES_data.csv")


## Garbage collection
#####################
rm(list=ls())




